package com.akxy.pt.tsdb.influxdb.operator;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Mengfly
 */
public class RangeOperator implements Operator {

    private static final ThreadLocal<SimpleDateFormat> DATE_LOCAL
            = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
    private static final ThreadLocal<SimpleDateFormat> TIME_LOCAL
            = ThreadLocal.withInitial(() -> new SimpleDateFormat("HH:mm:ss"));

    private final Date start;
    private final Date end;

    public RangeOperator(Date start, Date end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public String operatorString() {
        SimpleDateFormat dateFormat = DATE_LOCAL.get();
        SimpleDateFormat timeFormat = TIME_LOCAL.get();
        String startStr = String.format("%sT%sZ", dateFormat.format(start), timeFormat.format(start));
        String endStr = String.format("%sT%sZ", dateFormat.format(end), timeFormat.format(end));
        return String.format("|> range(start:%s, stop:%s)", startStr, endStr);
    }


}
